package cn.liyiming.bbs.core.interceptor;

import cn.liyiming.bbs.modular.system.model.User;
import cn.liyiming.bbs.util.CommonUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: bbsblog
 * @description: 对请求路径进行拦截
 * @author: 李一鸣
 * @create: 2020-03-04 17:08
 **/

public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getRequestURI().contains("login") || request.getRequestURI().contains("verifyLogin")|| request.getRequestURI().contains("index") || CommonUtil.releaseSuffix(request)) {
            return true;
        }
        User user = (User) request.getSession().getAttribute("user");

        if (request.getRequestURI().contains("api")) {
            response.setContentType("application/json;charset=utf-8");
            if ("".equals(user) || user == null) {
                request.setAttribute("msg", "没有权限登录");
                response.sendRedirect(request.getContextPath()+"/pages/index");
            } else {
                //放行
                return true;
            }
        } else {
            if (user == null) {
                //没有登录权限，返回登录界面
                response.sendRedirect(request.getContextPath()+"/pages/index");
            } else {
                return true;
            }
        }

        return false;
    }


}
